'''
Created on 28.05.2011

@author: blaze
'''
import sqlite3
from t2g.model.trac.wiki import WikiPage, WikiPages
from t2g.model.trac.ticket import TicketEntry, TicketManager
from t2g.model.trac.comment import CommentEntry, TicketComments

from t2g.utils.logger import logger

class DataReader(object):
    
    def __init__(self, location):
        self.dbLoc = location
                
    def readWikiPages(self):
        logger.info("Export wiki pages from trac database: %s" % self.dbLoc)
        dbConn = sqlite3.connect(self.dbLoc)#, encoding='utf-8')
        
        dbCursor = dbConn.cursor()
        dbCursor.execute("select * from wiki;")
        pages = WikiPages()
        
        counter= 0
        for row in dbCursor:
            counter += 1
            #Datafields map:
            # 0 - name
            # 1 - version
            # 2 - time
            # 3 - author
            # 4 - ipnr
            # 5 - text
            # 6 - comment
            # 7 - readonly
            #print ("%i: %s" % (counter, row))
            page = WikiPage(row[0], row[1], row[5], row[2], row[3], row[6])
            pages.addPage(page)            
            
        dbConn.close()
        
        logger.info("Exported %i wiki pages" % len(pages))
        return pages

    def readTickets(self):
        logger.info("Export issues from trac database: %s" % self.dbLoc)        
        dbConn = sqlite3.connect(self.dbLoc)#, encoding='utf-8')
        
        dbCursor = dbConn.cursor()
        dbCursor.execute("select id, type, time, severity, priority, owner, reporter, version,"
                 + "milestone, status, resolution, summary, description, keywords from ticket;")
        
        tickets = TicketManager()
        
        for row in dbCursor:
            ticket = TicketEntry(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], 
                                 row[8], row[9], row[10], row[11], row[12], row[13])
            ticket.addComments(self.readTicketComments(ticket.id))
            tickets.addTicket(ticket)
        dbConn.close()
        logger.info("Exported %i issues" % len(tickets))
        
        return tickets
    
    def readTicketComments(self, ticketId):
        logger.info("Export issue comments for issue %i from trac database: %s" % (ticketId, self.dbLoc))
        dbConn = sqlite3.connect(self.dbLoc)#, encoding='utf-8')
        
        dbCursor = dbConn.cursor()
        dbCursor.execute("select time, author, newvalue from ticket_change "
                         + "where ticket=%i and field='comment' and newvalue != '';" % ticketId)
        
        comments = TicketComments(ticketId)
        
        for row in dbCursor:
            comment = CommentEntry(ticketId, row[0], row[1], row[2])
            comments.addComment(comment)            
        dbConn.close()
        logger.info("Exported %i issue comments" % len(comments))
        
        return comments
    
if __name__ == "__main__":
    reader = DataReader('c:/temp/trac.db')
    tickets = reader.readTickets()
    
    for ticket in tickets.getTickets():
        ticket.addComments(reader.readTicketComments(ticket.id))
        print ticket
    tickets.printStat()
#    pages = reader.readWikiPages()
#    
#    for page in pages.getPagesName():
#        print (page)
#        for pageVer in pages.getPageVersions(page):            
#            print ("\t%s" % pageVer)
#    pages.printStat()